/**
 * Project  : monkey
 * Author   : Wu Tian Qiang
 * Date     : 2016/10/14 18:39:22
 */
Ext.define('Monkey.view.icon.IconController', {
    extend: 'Monkey.view.BaseController',
    alias: 'controller.ctrl-icon',

    init: function () {

    },

    reloadIcon: function () {
        var me = this;
        var store = me.getView().down('dataview').getStore();
        var iconType = me.getViewModel().data.iconType;
        store.loadPage(1);
        this.getReferences().iconName.setValue('');
    },

    findSIZE16Icons: function () {
        this.findIconsByType('SIZE16');
    },

    findSIZE64Icons: function () {
        this.findIconsByType('SIZE64');
    },

    findIconsByType: function (type) {
        var store = this.getView().down('dataview').getStore();
        this.getViewModel().data.iconType = type;
        store.getProxy().setExtraParam('search.iconType_eq', type);
        store.loadPage(1);
    },

    findIconByName: function () {
        var iconName = this.getReferences().iconName.getValue();
        var store = this.getView().down('dataview').getStore();
        var params = {};
        if (!Ext.isEmpty(iconName)) {
            params['search.name_like'] = iconName;
        }
        store.loadPage(1, {
            params: params
        });
    },

    synIcons: function () {
        var me = this;
        var view = this.getView();

        Ext.Msg.show({
            title: '请确认',
            message: '确认与文件系统同步图标数据吗？',
            buttons: Ext.Msg.OKCANCEL,
            fn: function (buttonId) {
                if (buttonId != 'ok') {
                    return;
                }
                view.mask('正在同步图标，请稍等...');
                me.request(Monkey.URL.SERVER_URL + '/m/syn_icons.json', 'GET', {}, function (data) {
                    view.unmask();
                    var icon = Ext.Msg.ERROR;
                    if (data.success) {
                        icon = Ext.Msg.INFO;
                        me.reloadIcon();
                    }
                    Ext.Msg.show({
                        title: '提示',
                        message: data.message,
                        buttons: Ext.Msg.OK,
                        icon: icon
                    });
                });
            },
            icon: Ext.Msg.INFO
        });


    },

    itemclick: function (cmp, record, item, index) {
        var copyIconName = this.getReferences().copyIconName;
        copyIconName.setValue('resources/icons/' + record.data.icon);
    }


});
